<template>
  <div id="fullscreenElement" :class="[theme, 'w-[100vw]', 'h-[100vh]']">
    <header class="headerFloatBar">
      <span id="siteName">看漫画</span>
      <span id="title">{{ currentTitle }}</span>
      <span class="sizeBtn" id="enterFullscreenButton" @click="setFullscreen"
        >全屏</span
      >
      <span class="separator">|</span>
      <span class="sizeBtn" @click="resize(1)">一屏一页</span>
      <span class="separator">|</span>
      <span class="sizeBtn" @click="resize(2)">一屏两页</span>
      <span class="separator">|</span>
      <span class="sizeBtn" @click="resize(3)">一屏三页</span>
      <button @click="scrollAuto">自动滚动</button>
      <button @click="minusSpeed" style="cursor: pointer">-</button>
      <span id="speed" style="color: white">{{ speed }}</span>
      <button @click="addSpeed" style="cursor: pointer">+</button>
      <button @click="backToUp">回到最上方</button>
    </header>
    <div id="content" class="flex" @mouseenter="hideFloatBar">
      <div id="directory"></div>
      <div id="picViewArea">
        <div id="imgBox" class="text-center"></div>
        <div id="bigImgBox" @click="hideImg">
          <img id="bigImg" />
        </div>
      </div>
    </div>
    <footer class="footerDisplayBar" @mouseenter="showFloatBar">
      展开工具栏
    </footer>
    <footer class="footerFloatBar">
      <span @click="switchDirectory">目录</span>
      <span @click="switchTheme">开关</span>
    </footer>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from "vue";

export default {
  setup() {
    const speed = ref(3);
    const theme = ref("dark");
    const currentTitle = ref("");
    const intervalId = ref(null);
    const scrollPosition = ref(0);
    const scrollSpeed = ref(10);

    const minusSpeed = () => {
      speed.value -= 0.5;
    };

    const addSpeed = () => {
      speed.value += 0.5;
    };

    const resize = (size) => {
      const imgBox = document.getElementById("imgBox");
      imgBox.className = "";
      if (size === 1) {
        imgBox.classList.add("img-1-column");
      }
      if (size === 2) {
        imgBox.classList.add("img-2-column");
      }
      if (size === 3) {
        imgBox.classList.add("img-3-column");
      }
    };

    const hideImg = () => {
      const bigImgBox = document.getElementById("bigImgBox");
      bigImgBox.style.display = "none";
    };

    const switchDirectory = () => {
      const directory = document.getElementById("directory");
      const directoryUl = document.getElementById("directoryUl");
      const picViewArea = document.getElementById("picViewArea");
      if (directory.style.width === "0px") {
        directory.style.width = "280px";
        picViewArea.style.width = "calc(100% - 280px)";
        directoryUl.style.display = "block";
      } else {
        directory.style.width = "0px";
        picViewArea.style.width = "100%";
        directoryUl.style.display = "none";
      }
    };

    const switchTheme = () => {
      theme.value = theme.value === "dark" ? "light" : "dark";
    };

    const showFloatBar = () => {
      const footerFloatBar = document.querySelector(".footerFloatBar");
      const footerDisplayBar = document.querySelector(".footerDisplayBar");
      footerFloatBar.style.bottom = "0px";
      footerDisplayBar.style.opacity = "0";
    };

    const hideFloatBar = () => {
      const footerFloatBar = document.querySelector(".footerFloatBar");
      const footerDisplayBar = document.querySelector(".footerDisplayBar");
      footerFloatBar.style.bottom = "-44px";
      footerDisplayBar.style.opacity = "1";
    };

    const scrollAuto = () => {
      const picViewArea = document.getElementById("picViewArea");
      const imgBox = document.getElementById("imgBox");
      scrollPosition.value = picViewArea.scrollTop;
      let lastTime = 0;

      const step = (timestamp) => {
        if (!lastTime) lastTime = timestamp;
        const progress = timestamp - lastTime;

        if (progress > scrollSpeed.value) {
          scrollPosition.value += speed.value;
          picViewArea.scrollTo(0, scrollPosition.value);
          lastTime = timestamp;
        }

        if (scrollPosition.value < imgBox.scrollHeight) {
          intervalId.value = requestAnimationFrame(step);
        }
      };

      if (intervalId.value) {
        cancelAnimationFrame(intervalId.value);
        intervalId.value = null;
      } else {
        intervalId.value = requestAnimationFrame(step);
      }
    };

    const backToUp = () => {
      const picViewArea = document.getElementById("picViewArea");
      picViewArea.scrollTo(0, 0);
    };

    const setFullscreen = () => {
      const fullscreenElement = document.getElementById("fullscreenElement");
      if (fullscreenElement.requestFullscreen) {
        fullscreenElement.requestFullscreen();
      } else if (fullscreenElement.mozRequestFullScreen) {
        fullscreenElement.mozRequestFullScreen();
      } else if (fullscreenElement.webkitRequestFullscreen) {
        fullscreenElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
      } else if (fullscreenElement.msRequestFullscreen) {
        fullscreenElement.msRequestFullscreen();
      }
    };

    const createDirectory = (obj, ele) => {
      const keys = Object.keys(obj);
      const ul = document.createElement("ul");
      ele.appendChild(ul);
      if (keys.length === 1 && keys[0] === "images") {
        return;
      } else {
        for (let key of keys) {
          const li = document.createElement("li");
          li.innerHTML = `<span>${key}</span>`;
          li.onclick = (e) => {
            e.stopPropagation();
            document.getElementById("picViewArea").scrollTo(0, 0);
            currentTitle.value = key;
            if (obj[key]?.images) {
              const imgBox = document.getElementById("imgBox");
              imgBox.classList.add("img-1-column");
              imgBox.innerHTML = "";
              for (let src of obj[key]?.images) {
                const img = document.createElement("img");
                img.src = src;
                img.style.width = "800px";
                img.ondblclick = (e) => {
                  e.preventDefault();
                  document.getElementById("bigImgBox").style.display = "flex";
                  document.getElementById("bigImg").src = src;
                };
                imgBox.appendChild(img);
              }
            } else {
              const imgBox = document.getElementById("imgBox");
              imgBox.innerHTML = "";
            }
          };
          if (key !== "images" && JSON.stringify(obj[key]) !== "{}") {
            ul.appendChild(li);
            createDirectory(obj[key], li);
          }
        }
      }
    };

    const fetchData = async () => {
      try {
        const response = await fetch("images.json", {
          headers: {
            "content-type": "application/json",
          },
          method: "GET",
          credentials: "include",
        });
        const data = await response.json();
        console.log("fetchData ~ data:", data);
        const directory = document.getElementById("directory");
        const ul = document.createElement("ul");
        ul.id = "directoryUl";
        createDirectory(data, ul);
        directory.appendChild(ul);
      } catch (error) {
        console.error("Error:", error);
      }
    };

    onMounted(() => {
      fetchData();
      document.addEventListener("keydown", (event) => {
        if (event.key === "+") {
          speed.value += 0.5;
        } else if (event.key === "-") {
          speed.value -= 0.5;
        } else if (event.key === " " || event.keyCode === 32) {
          event.preventDefault();
          scrollAuto();
        }
      });
    });

    onUnmounted(() => {
      if (intervalId.value) {
        cancelAnimationFrame(intervalId.value);
      }
    });

    return {
      speed,
      theme,
      currentTitle,
      minusSpeed,
      addSpeed,
      resize,
      hideImg,
      switchDirectory,
      switchTheme,
      showFloatBar,
      hideFloatBar,
      scrollAuto,
      backToUp,
      setFullscreen,
    };
  },
};
</script>

<style lang="less" scoped>
@darkTheme: #333;
@darkHeaderTheme: #333;
@darkFontColor: #ababab;
@darkSiteNameFontHoverColor: #f4e3ab;
@darkFontHoverColor: #e66201;

@lightTheme: #edecea;
@lightHeaderTheme: #f0f0f0;
@lightFontColor: black;
@lightSiteNameFontHoverColor: #f4e3ab;
@lightFontHoverColor: #e66201;

body {
  margin: 0;
}

ul {
  padding-inline-start: 15px;
  cursor: pointer;
}
.dark {
  background-color: @darkTheme;
  header {
    height: 55px;
    line-height: 55px;
    position: relative;
    cursor: pointer;
  }
  @keyframes showFloatBar {
    from {
      top: -55px;
    }
    to {
      top: 0px;
    }
  }
  @keyframes hideFloatBar {
    from {
      top: 0px;
    }
    to {
      top: -55px;
    }
  }
  .headerFloatBar {
    position: fixed;
    background-color: @darkHeaderTheme;
    top: 0px;
  }
  .showHeaderFloatBar {
    animation: showFloatBar 0.5s linear 0s;
  }
  .hideHeaderFloatBar {
    animation: hideFloatBar 0.5s linear 0s;
  }
  #siteName {
    font-size: 24px;
    color: @darkFontColor;
    padding-left: 20px;
    padding-right: 10px;
    &:hover {
      color: @darkSiteNameFontHoverColor;
    }
  }
  .sizeBtn {
    color: @darkFontColor;
    vertical-align: top;
    &:hover {
      color: @darkFontHoverColor;
    }
  }
  .separator {
    padding: 0 5px;
    color: @darkFontColor;
    vertical-align: top;
  }
  #title {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    color: @darkFontColor;
    &:hover {
      color: @darkFontHoverColor;
    }
  }
  #directoryUl {
    color: @darkFontColor;
    li span {
      &:hover {
        color: @darkFontHoverColor;
      }
    }
  }
  #content {
    position: absolute;
    top: 55px;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
  }
  footer {
    position: fixed;
    bottom: 0px;
    height: 44px;
    padding-left: 20px;
    width: 100%;
    color: @darkFontColor;
    cursor: pointer;
  }
  .footerFloatBar {
    bottom: -44px;
  }
}

.light {
  background-color: @lightTheme;
  header {
    height: 55px;
    line-height: 55px;
    position: relative;
    cursor: pointer;
  }
  @keyframes showFloatBar {
    from {
      top: -55px;
    }
    to {
      top: 0px;
    }
  }
  @keyframes hideFloatBar {
    from {
      top: 0px;
    }
    to {
      top: -55px;
    }
  }
  .headerFloatBar {
    position: fixed;
    background-color: @lightHeaderTheme;
    top: -55px;
  }
  .showHeaderFloatBar {
    animation: showFloatBar 0.5s linear 0s;
  }
  .hideHeaderFloatBar {
    animation: hideFloatBar 0.5s linear 0s;
  }
  #siteName {
    font-size: 24px;
    color: @lightFontColor;
    padding-left: 20px;
    padding-right: 10px;
    &:hover {
      color: @lightSiteNameFontHoverColor;
    }
  }
  .sizeBtn {
    color: @lightFontColor;
    vertical-align: top;
    &:hover {
      color: @lightFontHoverColor;
    }
  }
  .separator {
    padding: 0 5px;
    color: @lightFontColor;
    vertical-align: top;
  }
  #title {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    color: @lightFontColor;
    &:hover {
      color: @lightFontHoverColor;
    }
  }
  #directoryUl {
    color: @lightFontColor;
    li span {
      &:hover {
        color: @lightFontHoverColor;
      }
    }
  }
  #content {
    position: absolute;
    top: 55px;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
  }
  footer {
    position: fixed;
    bottom: 0px;
    height: 44px;
    padding-left: 20px;
    width: 100%;
    color: @lightFontColor;
    cursor: pointer;
  }
  .footerFloatBar {
    bottom: -44px;
  }
}

h2 {
  margin: 10px 0;
}

#imgBox {
  display: grid;
  grid-column-gap: 10px;
  grid-row-gap: 10px;
  place-items: center; /* 同时水平和垂直居中 */
}

.img-1-column {
  grid-template-columns: auto;
}
.img-2-column {
  grid-template-columns: auto auto;
}
.img-3-column {
  grid-template-columns: auto auto auto;
}

#bigImgBox {
  background-color: rgba(0, 0, 0, 0.5); /* 背景颜色为半透明的黑色 */
  position: fixed; /* 固定位置，不随页面滚动而移动 */
  z-index: 98; /* 设置极高的z-index值，确保蒙层覆盖在其他内容之上 */
  top: 0; /* 顶部位置为0 */
  bottom: 0; /* 底部位置为0 */
  left: 0; /* 左侧位置为0 */
  right: 0; /* 右侧位置为0 */
  display: none;
  justify-content: center; /* 水平居中对齐内容 */
  align-items: center; /* 垂直居中对齐内容 */
}
#bigImg {
  width: 800px;
  z-index: 99;
}

#directory {
  width: 280px;
  height: calc(100% - 54px);
  position: relative;
  overflow-y: scroll;
}

#picViewArea {
  width: calc(100% - 280px);
  height: 100%;
  overflow-y: scroll;
}

#switch {
  position: fixed;
  left: 190px;
  bottom: 10px;
  background-color: white;
  border: 1px solid gray;
  border-radius: 100%;
  display: flex;
  align-items: center;
  width: 28px;
  height: 28px;
}

/* 整体滚动条样式 */
::-webkit-scrollbar {
  width: 5px; /* 滚动条的宽度 */
}

/* 滚动条轨道样式 */
::-webkit-scrollbar-track {
  background: #f1f1f1; /* 轨道颜色 */
  border-radius: 10px;
}

/* 滚动条滑块样式 */
::-webkit-scrollbar-thumb {
  background: #888; /* 滑块颜色 */
  border-radius: 10px; /* 滑块圆角 */
}

/* 鼠标悬停在滑块上时的样式 */
::-webkit-scrollbar-thumb:hover {
  background: #555; /* 鼠标悬停时的滑块颜色 */
}
</style>
